var itemType = require('item.js');
var colorCal = require('color.js');
var itemcolorCal = colorCal.itemColor;
var util = require('../utils/util.js');
var app = getApp();
(function() {
    var todoItem = itemType.createItem();
    todoItem.activate = function() {
        if (this.activated) return;
        this.activated = true;
        if (this.done) {
            this.updateColor(this.list.count);
            //this.setLineStyle('none');
        }
        else {
            this.setColor(util.COLOR_DONE);
            //this.setLineStyle('line-through');
        }
    };
    todoItem.unActivate = function() {
        if (!this.activated) return;
        this.activated = false;
        if (this.done) {
            //this.setLineStyle('line-through');
            this.setColor(util.COLOR_BLACK);
        }
        else {
            //this.setLineStyle('none');
            this.updateColor(this.list.count);
        }
    };
    todoItem.updateColor = function(count) {
        if (this.deleted) {
            return;
        }
        if (this.done) {
            this.sliderColor = util.COLOR_BLACK;   
        }
        else {
            this.sliderColor = itemcolorCal.calItemColor(this.getOrder(), count);
        }
        this.render('sliderColor', this.sliderColor);
    };
    todoItem.calColor = function(count) {
        if (this.done) {
            this.sliderColor = util.COLOR_BLACK;
        }
        else {
            this.sliderColor = itemcolorCal.calItemColor(this.getOrder(), count);
        }     
    };
    todoItem.setColor = function(color) {
        this.sliderColor = color;
        this.render('sliderColor', color);
    };
    todoItem.onTap = function() {
        if (!this.done) {
            this.onEdit();
        }
        else {
            this.list.onTap();
        }
    };
    todoItem.complete = function() {
        if (!this.done) {
            this.beDone();
        }
        else {
            this.unDone();
        }
    };
    todoItem.doDone = function() {
        if (this.done) {
            return;
        }
        this.setColor(util.COLOR_BLACK);
        this.setLineStyle('line-through');
        this.setTextColor(util.COLOR_GRAY);
        this.data.state = util.STATE_DONE;
        this.done = true;
    };
    todoItem.beDone = function() {
        this.done = true;
        this.data.state = util.STATE_DONE;
        this.list.doneItem(this); 
    };
    todoItem.unDone = function() {
        this.done = false;
        this.data.state = util.STATE_NORMAL;
        this.list.unDoneItem(this);
    };
    todoItem.del = function() {
        var item = this;
        this.disappear(function() {
            item.deleted = true;
            item.list.delItem(item);
        });
    };
    todoItem.touchmove = function(dx) {
        this.ontouchmove(dx);
        if (this.sliderX >= app.checkBound) {
            this.activate();
        }
        else {
            this.unActivate();
        }
        this.page.render();
    };

    todoItem.touchend = function() {
        this.ontouchend();
    };

    todoItem.setSortItemStyle = function() {
        var sortItem = this.list.sortItem;
        sortItem.setStyle(this.height, this.getText(), 
            this.textColor, this.sliderColor, this.lineStyle);
    };

    todoItem.onsortend = function() {
        if (this.done) {
            if (this.getOrder() < this.list.count) {
                this.done = false;
                this.data.state = util.STATE_NORMAL;
                this.setTextColor(util.COLOR_WHITE);
                this.setLineStyle('none');
                this.list.count++;
                this.updateColor(this.count);
                this.list.updateCount();
            }
        }
        else {
            if (this.getOrder() >= this.list.count) {
                this.done = true;
                this.data.state = util.STATE_DONE;
                this.setTextColor(util.COLOR_GRAY);
                this.setLineStyle('line-through');
                this.setColor(util.COLOR_BLACK);
                this.list.count--;
                this.list.updateCount();
            }
        }
    };

    function createItem() {
        return Object.create(todoItem);
    }

    module.exports = {
        createItem:createItem
    }
})();